package com.zheng.controller;


import com.alibaba.fastjson.JSON;
import com.zheng.entity.User;
import com.zheng.service.UserService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/test")
    public List<User> list(){
        return userService.list();
    }

    @PostMapping("/redisTest/add")
    public String redisTestAdd(){
        return userService.redisTestAdd();
    }

    @PostMapping("/redisTest/expir")
    public String redisTestExpir(){
        return userService.redisTestExpir();
    }

    @PostMapping("/test1")
    public String test1(){
        System.out.println("123");
        userService.test1();
        return "456";
    }

    @GetMapping("/test3")
    public void test3(HttpServletResponse response){
        System.out.println(":123");
        String templateName = "aaa.xlsx";
        ClassPathResource resource = new ClassPathResource("files/" + templateName,
                Thread.currentThread().getContextClassLoader());
        try (InputStream inputStream = resource.getInputStream(); OutputStream outputStream = response.getOutputStream()) {
            response.setStatus(200);
            response.setContentType("application/octet-stream");
            response.addHeader("Connection", "close");
            response.addHeader("Accept-Ranges", "bytes");
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(templateName, "UTF-8"));
            IOUtils.copy(inputStream, outputStream);
        } catch (Exception e) {
            System.out.println("措意");
        }

        System.out.println("成功");
    }


    @GetMapping("/test6")
    public void test6(HttpServletResponse response) {
        String templateName = "aaa.xlsx";
        String msg = "http://hose-mall-image.oss-cn-beijing.aliyuncs.com/mall-oss/oss-excel-template/%E8%B4%A6%E5%8D%95%E6%89%93%E5%8C%85%E6%A0%B7%E5%BC%8F.xlsx";

        try (InputStream inputStream = new URL(msg).openStream();
             Workbook workbook = WorkbookFactory.create(inputStream); // 自动识别格式
             OutputStream outputStream = response.getOutputStream()) {

            Sheet sheet = workbook.getSheetAt(0);
            System.out.println("Sheet Name: " + sheet.getSheetName());
            System.out.println("First Row: " + sheet.getFirstRowNum());
            System.out.println("Last Row: " + sheet.getLastRowNum());

            // 如果仍然返回-1，说明文件可能有问题
            if (sheet.getLastRowNum() < 0) {
                throw new RuntimeException("Excel 文件没有可读取的行，可能格式错误或损坏！");
            }

            // 获取或创建第2行
            Row row = sheet.getRow(1);
            if (row == null) {
                row = sheet.createRow(1);
            }

            // 写入数据
            row.createCell(0).setCellValue("你好");

            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(templateName, "UTF-8"));

            workbook.write(outputStream);
            System.out.println("成功");
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(500);
        }
    }

}
